<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>localStorage</title>
    <script type="text/javascript">
        window.onload=function () {
            var sname = localStorage.getItem("keyname");
            var spass = localStorage.getItem("keypass");
            if (sname) {
                document.getElementById("name").value = sname;
                document.getElementById("pass").value = spass;
            }
            document.getElementById("btn").onclick = function () {
                var sname = document.getElementById("name").value;
                var spass = document.getElementById("pass").value;
                localStorage.setItem("keyname", sname);
                if (document.getElementById("save").checked) {
                    localStorage.setItem("keypass", spass);
                }
                else {
                    localStorage.removeItem("keypass");
                }
                window.alert("登录成功！");
            }
        }
    </script>
</head>
<body>
<fieldset>
    <legend>登录</legend>
    <label>名称：<input type="text" id="name"></label><br>
    <label>密码：<input type="password" id="pass"></label><br>
    <input type="checkbox" id="save">是否保存密码<br>
    <input type="button" id="btn" value="登录">
    <input type="reset" id="ret" value="取消">
</fieldset>
</body>
</html>